package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.common.LayuiPageVo;

import com.liuzu.esys.mapper.VipmemberMapper;
import com.liuzu.esys.pojo.*;

import com.liuzu.esys.service.VipmemberService;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 会员特价层 实现类
 */
@Service("VipmemberService")
public class VipmemberServiceImpl implements VipmemberService {
        @Autowired
        private VipmemberMapper vipmemberMapper;

    @Override
    public LayuiPageVo<Vipmember> pagingquery(Map<String, Object> params) {
        //mybatis 的 分页查询
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);

        List<Vipmember> vipmembers = vipmemberMapper.vipmemberList(params);

        //mybatis 的 分页数据结构
        PageInfo<Vipmember> studentPageInfo = new PageInfo<>(vipmembers);
        //把 mybatis 的分页数据封装到 layui的分页数据结构中

        LayuiPageVo<Vipmember> studentLayuiPageVo = new LayuiPageVo<>();
        studentLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        studentLayuiPageVo.setMsg("分页列表数据"); // 提示信息
        studentLayuiPageVo.setCount(studentPageInfo.getTotal()); // 总记录数
        studentLayuiPageVo.setData(studentPageInfo.getList());  // 分页的列表数据
        return studentLayuiPageVo;
    }

    @Override
    @Transactional(rollbackFor = RuntimeSqlException.class,propagation = Propagation.REQUIRED)
    public boolean vipmemberAdd(Vipmember vipmember) {
        boolean flag = false;

        //查询 goods 里面是否有这个物品名字
       Good good  = vipmemberMapper.selectGoodsName(vipmember.getName());
        //查询 特价商品里面是否有这个物品名字
        Vipmember tmpe = vipmemberMapper.selectvipmember(vipmember.getName());
        if(good == null || tmpe != null){
            throw new RuntimeSqlException();

        }
        vipmember.setPrice(good.getPrice());
        vipmember.setVip_1_price((double) (good.getPrice()*0.9));
        vipmember.setVip_2_price((double) (good.getPrice()*0.8));
        vipmember.setVip_3_price((double) (good.getPrice()*0.7));
        vipmember.setVip_4_price((double) (good.getPrice()*0.6));
        vipmember.setType(good.getType());
        //测试这边数值是否都正确
        //System.out.println(vipmember);
        int a = vipmemberMapper.vipmemberAdd(vipmember);
       if(a==1){
           flag = true;
       }else {
           throw new RuntimeSqlException();
       }
        return flag;
    }
    //根据 传来 id 删除操作 删除特价商品
    @Override
    public int vipmemberDel(Integer id) {
     return  vipmemberMapper.vipmemberDel(id);
    }

    @Override
    public boolean vipmemberByIds(String ids) {
        String [] arrId = ids.split(",");
        boolean flag = false;
         int a = vipmemberMapper.vipmemberByIds(arrId);
         if(a > 0){
             flag = true;
             return flag;
         }else {
             return flag;
         }

    }
    //校验功能 根据传来的名字查询是否可以用
    @Override
    public Good vipmemberExamineName(String name) {
       Good good =  vipmemberMapper.vipmemberExamineName(name);
        return good;
    }
    //校验功能 根据传来的名字查询是否可以用
    @Override
    public Vipmember vipmemberName(String name) {
        Vipmember vipmember = vipmemberMapper.vipmemberName(name);
        return vipmember;
    }
    //


}
